﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LuKou.Member;
using LuKou.Shopspace;
using LuKou.Shopspace.Domains;
using LuKou.Shopspace.Services;
using LuKou.Web.BLL.Extensions;

namespace LuKou.Web.BLL.Service
{
    public class ShopService
    {
        private static IShopSvr _svr;
        static ShopService()
        {
            _svr = ServiceHelper.Get<IShopSvr>("IShopSvr");
        }

        public static IShopSvr Instance()
        {
            return _svr;
        }

        public static Shop GetShop(long shopId)
        {
            return _svr.GetShopDetail(shopId, true);
        }

        /// <summary>
        /// 根据用户ID获取店铺信息
        /// </summary>
        /// <param name="uid">用户ID</param>
        /// <returns></returns>
        public static Shop GetShopByUserID(long uid)
        {
            ShopQueryParam param = new ShopQueryParam();
            param.AddParamOfUserID(uid);
            var list = _svr.GetShopList(param, true, 0, 1);

            return (list != null && list.Count > 0) ? (Shop)list[0] : null;
        }

        /// <summary>
        /// 更新店铺信息
        /// </summary>
        /// <param name="shop"></param>
        /// <param name="uid"></param>
        public static void UpdateShopInfo(Shop shop, long uid)
        {
            shop.UserID = uid;
            _svr.ModifyShop(shop);
        }

        public static int GetShopCount(EShopType? shopType)
        {
            ShopQueryParam param = new ShopQueryParam();
            if (shopType.HasValue)
            {
                param.AddParamOfShopType((int)shopType);
            }
            return _svr.GetShopCount(param);
        }

        /// <summary>
        /// 获取店铺列表
        /// </summary>
        /// <param name="index">页数</param>
        /// <param name="pageSize">每页大小</param>
        /// <param name="orderFields">排序字段</param>
        /// <returns></returns>
        public static PagedList<Shop> GetShopList(int index, int pageSize, IList<String> orderFields)
        {
            return GetShopList(null, index, pageSize, orderFields);
        }

        /// <summary>
        /// 获取店铺列表
        /// </summary>
        /// <param name="shopType">店铺类型</param>
        /// <param name="index">页数</param>
        /// <param name="pageSize">每页大小</param>
        /// <param name="orderFields">排序字段</param>
        /// <returns></returns>
        public static PagedList<Shop> GetShopList(EShopType? shopType, int index, int pageSize, IList<String> orderFields)
        {
            ShopQueryParam param = new ShopQueryParam();
            if (shopType.HasValue)
            {
                param.AddParamOfShopType((int)shopType);
            }

            if (orderFields != null && orderFields.Count > 0)
            {
                foreach (var orderField in orderFields)
                {
                    param.SetOrderByField(orderField);
                }
            }

            if (index < 1) index = 1;
            if (pageSize < 1) pageSize = 10;

            IList<Shop> shoplist = _svr.GetShopList(param, true, (index - 1) * pageSize, pageSize).Cast<Shop>().ToList();

            return new PagedList<Shop>(shoplist, index, pageSize, _svr.GetShopCount(param));
        }

        /// <summary>
        /// 获取会员记录条数
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public static int GetVendorShopMemberCount(VendorShopMemberQueryParam param)
        {
            return _svr.GetVendorShopMemberCount(param);
        }

        /// <summary>
        /// 获取会员记录
        /// </summary>
        /// <param name="index"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public static PagedList<VendorShopMember> GetVendorShopMemberList(int index, int pageSize)
        {
            if (index < 1) index = 1;
            if (pageSize < 1) pageSize = 10;
            List<VendorShopMember> result = _svr.GetVendorShopMemberList(null, true, (index - 1) * pageSize, pageSize).Cast<VendorShopMember>().ToList();
            result = new List<VendorShopMember>()
            {
                new VendorShopMember(){ ID=1,AgentsUserID=7,AgentsGradeID=2}
            };
            return new PagedList<VendorShopMember>(result, index, pageSize, _svr.GetVendorShopMemberCount(null));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id">代理用户ID</param>
        /// <returns></returns>
        public static AgentsInfo GetAgentsInfo(long id)
        {
            return _svr.GetAgentsInfo(id);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="info"></param>
        public static void SetAgentsInfo(AgentsInfo info)
        {
            _svr.SetAgentsInfo(info);
        }
    }
}
